/*
this program is used to send msg to trigger the ele machine operation
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>

#include <sys/stat.h>
#include <fcntl.h>

#include <linux/can.h>
#include <linux/can/raw.h>

#include "../app/can_uim.h"
#include "../app/msg_def.h"
#include "./data_proc/board_data_proc.h"

#define FILE_NAME "sample.txt"


int main(int argc, char *argv[])
{
    FILE * fp;
    unsigned char sid = 0;
    unsigned char tcw = 0;
    int flag_file = 0;
    int dlc_f = 0;
    unsigned char data_f[8];

    struct can_frame canFrame;

    sample_queue_msg ctlFrame;

    memset(&ctlFrame,0,sizeof(sample_queue_msg));

    sample_msg msgFrame;

    int flagValid = 0;

    if(argc<2)
    {
        fp = fopen(FILE_NAME,"r");
        

    }
    else
    {
        fp = fopen(argv[1],"r");
    }
    if(fp!=NULL)
    {
        char StrLine[256];
        char * tmpPtr = NULL;
        while(!feof(fp))
        {
           memset(StrLine,0,256);
           fgets(StrLine,256,fp);
           if(strstr(StrLine,"op"))
           {
               tmpPtr = StrLine+3;
               if(strstr(tmpPtr,"start"))
               {
                   //printf("set to start sample(%d)\n",START_SAMPLE);
                   ctlFrame.sampleMsg.act_type = START_SAMPLE;
               }
               else if(strstr(tmpPtr,"stop"))
               {
                   //printf("set to stop sample\n");
                   ctlFrame.sampleMsg.act_type = STOP_SAMPLE;
               }
               continue;
           }

           if(strstr(StrLine,"datatype"))
           {
               tmpPtr = StrLine+9;
               if(strstr(tmpPtr,"cap"))
               {
                   ctlFrame.sampleMsg.data_type = CAPACITY_SAMPLE;
               }
               else if(strstr(tmpPtr,"temp"))
               {
                   ctlFrame.sampleMsg.data_type = TEMPRATURE_SAMPLE;
               }
               continue;
           } 

           if(strstr(StrLine,"board"))
           {
               tmpPtr = StrLine+6;
               if(strstr(tmpPtr,"cap"))
               {
                   ctlFrame.sampleMsg.boardId = CAP_DETECT_BOARD;
               }
               else if(strstr(tmpPtr,"purify"))
               {
                   ctlFrame.sampleMsg.boardId = PURIFICATION_BOARD;
               }
               else if(strstr(tmpPtr,"thermal"))
               {
                   ctlFrame.sampleMsg.boardId = THERMAL_SEAL_BOARD;
               }
               else if(strstr(tmpPtr,"system"))
               {
                   ctlFrame.sampleMsg.boardId = SYSTEM_CTRL_BOARD;
               }
               continue;
           }

           if(strstr(StrLine,"chan"))
           {
               tmpPtr = StrLine+5;
               ctlFrame.sampleMsg.chan = strtol(tmpPtr,NULL,16);
               continue;
           }           

        }
        
        

        //  printf("param from file: sid:%d,tcw:%d,dlc_f:%d\n",sid,tcw,dlc_f);
        //  printf("speed:%d,inc_speed:%d,dec_speed:%d\n",
        //  ctlFrame.ctlFrame.un_cmd.moveCtl.speed,
        //  ctlFrame.ctlFrame.un_cmd.moveCtl.inc_speed,
        //  ctlFrame.ctlFrame.un_cmd.moveCtl.dec_speed);
        // char strPrint[128];
        // int n=0, n1=0;
        // memset(strPrint,0,128);
        // if(dlc_f)
        // {
        //    n = snprintf(strPrint,128,"%02x ",data_f[0]);
        //    for(int i=1;i<dlc_f;i++)
        //    {
        //        n1 = snprintf(strPrint+n,128-n,"%02x ",data_f[i]);
        //        n += n1;
        //    }
        //    printf("data:%s\n",strPrint);        

        // }
        
    }
    else
    {
        /* code */
        printf("open file failed\n");
    }

    key_t key = ftok(msgQueueKeyPath, 0x1234);
    if(key < 0)
    {
        perror("ftok");
        printf("%s\n",msgQueueKeyPath);
        return -1;
    }

    int msg_id = msgget(key, IPC_CREAT|0600);//IPC_CREAT|0600
    if(msg_id < 0)
    {
        perror("msgget");
        return -1;
    }

    ctlFrame.type = QUEUE_MSG_TYPE_SAMPLE;


    if(msgsnd(msg_id, (void*)&ctlFrame, sizeof(sample_msg), 0) < 0)
    {
        perror("msgsnd");
        return -1;
    }
    else
    {
        /* code */
        printf("msg send success,type is %d,opcode[%d]\n",ctlFrame.type,ctlFrame.sampleMsg.act_type);
    }        
    
    //msgctl(msg_id,IPC_RMID,NULL);

    return 0;
} 
/*
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <time.h>

#define TEXT_SIZE 512

struct _msgbuf
{

    long mtype;
    int status;
    char time[20];
    char mtext[TEXT_SIZE];    
};

char *getxtsj()
{
    time_t tv;
    struct tm *tmp;
    static char buf[20];

    tv = time( 0 );

    tmp = localtime(&tv);

    sprintf(buf,"%02d:%02d:%02d",tmp->tm_hour , tmp->tm_min,tmp->tm_sec);
    return buf;
}

int main(int argc, char **argv)

{

    int msqid ;

struct msqid_ds info;

struct _msgbuf buf;

struct _msgbuf buf1;

int flag ;

int sendlength, recvlength ;

int key ;



key = ftok("msg.tmp", 0x01 ) ;

if ( key < 0 )

{

perror("ftok key error") ;

return -1 ;

}



msqid = msgget( key, 0600|IPC_CREAT ) ;

if ( msqid < 0 )

{

 perror("create message queue error") ;
return -1 ;

}



buf.mtype = 1;

buf.status = 9;

strcpy(buf.time, getxtsj()) ;

strcpy(buf.mtext, "happy new year!\n") ;

sendlength = sizeof(struct msgbuf) - sizeof(long) ;

flag = msgsnd( msqid, &buf, sendlength , 0 ) ;

if ( flag < 0 )

{

perror("send message error") ;

return -1 ;

}

buf.mtype = 3 ;

buf.status = 9 ;

strcpy(buf.time, getxtsj()) ;

strcpy(buf.mtext, "good bye!") ;

sendlength = sizeof(struct msgbuf) - sizeof(long) ;

flag = msgsnd( msqid, &buf, sendlength , 0 ) ;

if ( flag < 0 )

{

perror("send message error") ;

return -1 ;

}
system("ipcs -q") ;

return 0 ;

}*/